package com.jfs.zlk.provider;

import cn.hutool.core.collection.CollectionUtil;
import com.jfs.zlk.config.ZlkMediaServerInfo;
import com.jfs.zlk.entity.request.IsMediaOnline;
import com.jfs.zlk.entity.request.RequestOpenRtpServer;
import com.jfs.zlk.entity.response.ResponseOpenRtpServer;
import com.jfs.zlk.properties.ZlkMediaServerProperties;
import com.jfs.zlk.service.ZlkMediaServerService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

/**
 * @Description zlk服务器功能提供者
 * @E-mail cwm3412638@163.com
 * @Author cwm
 * @Date 2022/7/27
 */
@Component
@AllArgsConstructor
public class ZlkMediaServerAbilityProvider {
    private static int CURRENT_PORT = 0;
    private static final int[] UDP_PORT_RANGE_ARRAY = new int[2];
    private final ZlkMediaServerProperties zlkMediaServerProperties;
    private final ZlkMediaServerInfo zlkMediaServerInfo;

    private final ZlkMediaServerService zlkMediaServerService;

    private final List<Integer> RABFE_PROTS = new ArrayList<Integer>();

    /**
     * 是否开启动态端口
     *
     * @return
     */
    public boolean getRtpEnable() {
        return this.zlkMediaServerProperties.getRtpEnable();
    }

    /**
     * 获取rtp默认代理端口
     *
     * @return
     */
    public String getRtpProxyPort() {
        return this.zlkMediaServerInfo.getRtpProxyPort();
    }

    /**
     * 获取是否开启sdp
     *
     * @return
     */
    public boolean getSdpEnable() {
        return this.zlkMediaServerProperties.isSdpEnable();
    }

    /**
     * 获取流媒体服务器ip
     *
     * @return
     */
    public String getMediaServerIp() {
        return this.zlkMediaServerProperties.getIpAddress();
    }


    /**
     * 判断直播流是否在线
     * @param isMediaOnline
     * @return
     */
    public Boolean isMediaOnline(IsMediaOnline isMediaOnline){
        return this.zlkMediaServerService.isMediaOnline(isMediaOnline);
    }

    public ResponseOpenRtpServer createRTPServer(String streamId) {
        //获取随机端口
        //int newPort = getPortFromUdpPortRange();
        Integer newPort = getPortFromUdpPortRange();
        RequestOpenRtpServer requestOpenRtpServer = new RequestOpenRtpServer();
        requestOpenRtpServer.setSecret(zlkMediaServerProperties.getSecret());
        requestOpenRtpServer.setPort(newPort);
        requestOpenRtpServer.setEnableTcp(1);
        requestOpenRtpServer.setStreamId(streamId);
        return zlkMediaServerService.openRtpServer(requestOpenRtpServer);
    }

    /**
     * 获取zlk服务器秘钥
     *
     * @return
     */
    public String getMeiaServerSecret() {
        return this.zlkMediaServerProperties.getSecret();
    }

    /**
     * 获取udp动态随机端口
     *
     * @return
     */
   /* private final int getPortFromUdpPortRange() {
        if (CURRENT_PORT == 0) {
            String[] udpPortRangeStrArray = zlkMediaServerProperties.getUdpPortRange().split(",");
            UDP_PORT_RANGE_ARRAY[0] = Integer.parseInt(udpPortRangeStrArray[0]);
            UDP_PORT_RANGE_ARRAY[1] = Integer.parseInt(udpPortRangeStrArray[1]);
        }

        if (CURRENT_PORT == 0 || CURRENT_PORT++ > UDP_PORT_RANGE_ARRAY[1]) {
            CURRENT_PORT = UDP_PORT_RANGE_ARRAY[0];
            return UDP_PORT_RANGE_ARRAY[0];
        } else {
            if (CURRENT_PORT % 2 == 1) {
                CURRENT_PORT++;
            }
            return CURRENT_PORT++;
        }
    }*/

    /**
     * 初始化可以随机的端口
     */
    public void initRangeProts() {
        String[] udpPortRangeStrArray = zlkMediaServerProperties.getUdpPortRange().split(",");

        for (UDP_PORT_RANGE_ARRAY[0] = Integer.parseInt(udpPortRangeStrArray[0]),
                     UDP_PORT_RANGE_ARRAY[1] = Integer.parseInt(udpPortRangeStrArray[1]);
             UDP_PORT_RANGE_ARRAY[0] <= UDP_PORT_RANGE_ARRAY[1];
             UDP_PORT_RANGE_ARRAY[0]++) {
            RABFE_PROTS.add(UDP_PORT_RANGE_ARRAY[0]);
        }
    }

    public Integer getPortFromUdpPortRange() {
        if (CollectionUtil.isNotEmpty(RABFE_PROTS) && RABFE_PROTS.size() > 0) {
            Random random = new Random();
            int num = random.nextInt(RABFE_PROTS.size());
            return RABFE_PROTS.get(num);
        }
        return null;
    }

    /**
     * 移除集合中元素
     * @param num
     */
    public void removePortRange(Integer num) {
        int i = RABFE_PROTS.indexOf(num);
        RABFE_PROTS.remove(i);
    }

    public void addProtRange(Integer num){
        RABFE_PROTS.add(num);
    }
}
